//
//  XHCollectionViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-16.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

//
//  XHCollectViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-5-26.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHCollectionViewController.h"
#import "XHCollectViewModel.h"



#define HEIGHT (([[UIDevice currentDevice].systemVersion floatValue] < 7.0 )?44.0:64.4)
@interface XHCollectionViewController ()
@property (nonatomic ,strong) MBProgressHUD *HUD;
@property (nonatomic ,assign) NSInteger totalPage;//  新闻总页数
@property (nonatomic ,assign) NSInteger currentPage;// 当前页数
@property (nonatomic ,assign) NSInteger count;// 新闻总条数
@property (nonatomic ,assign) NSInteger prev;// 前一页
@property (nonatomic ,assign) NSInteger next;// 后一页

@end

@implementation XHCollectionViewController
@synthesize collectViewModel=_collectViewModel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.frame = [[UIScreen mainScreen] bounds];
        self.view.backgroundColor = [UIColor whiteColor];
        self.tableView.pullDelegate = self;
        self.titleLable.text = @"我的收藏";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _collectViewModel = [[XHCollectViewModel alloc] init];
    // Do any additional setup after loading the view.
    
    
    [[_collectViewModel getCollectItemWithCurrentPage:1 pageSize:10]subscribeNext:^(id data) {
        
        if ( [data isKindOfClass:[NSString class]]) {
            
            [self performSelectorOnMainThread:@selector(showMessage:) withObject:data waitUntilDone:YES];
            
            return;
        }
        if ([data isKindOfClass:[NSDictionary class]]) {
            [_HUD hide:YES];
            [_HUD removeFromSuperview];
            
            self.dataArray = [data objectForKey:@"content"];
            _currentPage = 1;
            _next = [[data objectForKey:@"next"]  integerValue];
            _prev = [[data objectForKey:@"prev"] integerValue];
            [self performSelectorOnMainThread:@selector(reloadAllView) withObject:nil waitUntilDone:NO];
            
        }
        
        
        
    }];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)reloadAllView
{
    [self.tableView reloadData];
    [self.view setNeedsDisplay];
}
#pragma mark - Refresh and load more methods

/*******************刷新最新数据************************/

- (void) refreshTable
{
    
    
    
    
    [[_collectViewModel getCollectItemWithCurrentPage:1 pageSize:10] subscribeNext:^(id data) {
        
        
        if ([data isKindOfClass:[NSString class]]) {
            [self performSelectorOnMainThread:@selector(showMessage:) withObject:data waitUntilDone:YES];
            return ;
            
        }
        NSArray *array = [data objectForKey:@"content"];
        self.dataArray = [NSMutableArray arrayWithArray:array];
    }];
    
    
    self.tableView.pullLastRefreshDate = [NSDate date];
    self.tableView.pullTableIsRefreshing = NO;
}

- (void) loadMoreDataToTable
{
    if (_currentPage != _next) {
        [[_collectViewModel getCollectItemWithCurrentPage:_next pageSize:10] subscribeNext:^(id data) {
            NSMutableArray *dataArray = [[NSMutableArray alloc] initWithArray:self.dataArray];
            NSArray *array = [data objectForKey:@"content"];
            if ([array count] >0) {
                [dataArray addObjectsFromArray:array];
                self.dataArray = dataArray;
                _currentPage = _next;
                _next = [[data objectForKey:@"next"] integerValue];
                
            }
        }];
    }else{
        NSString *message = [self.dataArray count]>0?@"已全部加载":@"尚未登录";
        
        [self performSelectorOnMainThread:@selector(showMessage:) withObject:message waitUntilDone:YES];
        
    }
    
    self.tableView.pullTableIsLoadingMore = NO;
}

#pragma mark - PullTableViewDelegate

- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    
    
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0f];
}



@end
